import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'

import '@/icons' // icon
import '@/permission' // permission control
import filters from './filters'
import deleteNull from '@/api/common'

import { getLocalUid, setLocalUid, getLocalId, setLocalId } from '@/utils/uid'
import { getPermission } from '@/api/permission'
import { Message } from 'element-ui'
import VueCropper from 'vue-cropper'

// import ECharts from 'vue-echarts'
// import 'echarts/lib/chart/bar'
// import 'echarts/lib/component/tooltip'

// Vue.component('v-chart', ECharts)

import echarts from 'echarts'

Vue.prototype.$echarts = echarts

Vue.use(VueCropper)
Vue.use(deleteNull)

Object.keys(filters).forEach(k => Vue.filter(k, filters[k]))

Vue.use(ElementUI, { zhLocale })
Vue.config.productionTip = false
// 防抖自定义指令
Vue.directive('preventReClick', {
  inserted(el, binding) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true
        setTimeout(() => {
          el.disabled = false
        }, binding.value || 1000)
      }
    })
  }
})
// 删除省市区中不需要对城市
Vue.prototype.deletCityCode = function(regionData, codeArr) {
  for (let i = 0; i < regionData.length; i++) {
    for (let j = 0; j < codeArr.length; j++) {
      if (regionData[i].label === codeArr[j]) {
        regionData.splice(i, 1)
      }
    }
  }
}

Vue.prototype.transferText = function(value) {
  value = value.replace('[', '%5B').replace(']', '%5D')
  return value
}

Vue.prototype.objectCon = function(listQuery, search) {
  const newData = listQuery
  for (const key in search) {
    newData[key] = search[key]
  }
  return newData
}
Vue.prototype.validForbid = function(value) {
  value = value.replace(/[`～*~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]/g, '').replace(/\s/g, '');
  console.log(value)
  return value;
}

Vue.prototype.formatDateMinute = function(value) {
  if (value) {
    const date = new Date(value);
    const y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  } else {
    return ''
  }
}

Vue.prototype.getPermissonFn = function() {
  const pid = getLocalId()
  const uid = getLocalUid()
  console.log('接收的id：', pid)
  console.log('接收的uid：', uid)
  // const data = { uid }
  getPermission({ pid, uid }).then(res => {
    console.log('权限结果', res)
    if (res.code === '200') {
      console.log('dispatch前：', res.data)
      this.$store.dispatch('getUserBtn', res.data)
      console.log('菜单赋值之后：', this.$store.getters.button)
    } else {
      Message.error(res.message + '-' + res.data)
    }
  })
}

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
